{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Z字行变换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将一个给定字符串根据给定的行数，以从上往下、从左到右进行 Z 字形排列。\n",
    "\n",
    "比如输入字符串为 \"LEETCODEISHIRING\" 行数为 3 时，排列如下：\n",
    "\n",
    "L   C   I   R\n",
    "E T O E S I I G\n",
    "E   D   H   N\n",
    "之后，你的输出需要从左往右逐行读取，产生出一个新的字符串，比如：\"LCIRETOESIIGEDHN\"。\n",
    "\n",
    "请你实现这个将字符串进行指定行数变换的函数：\n",
    "\n",
    "string convert(string s, int numRows);\n",
    "示例 1:\n",
    "\n",
    "输入: s = \"LEETCODEISHIRING\", numRows = 3\n",
    "输出: \"LCIRETOESIIGEDHN\"\n",
    "示例 2:\n",
    "\n",
    "输入: s = \"LEETCODEISHIRING\", numRows = 4\n",
    "输出: \"LDREOEIIECIHNTSG\"\n",
    "解释:\n",
    "\n",
    "L     D     R\n",
    "E   O E   I I\n",
    "E C   I H   N\n",
    "T     S     G\n",
    "\n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/zigzag-conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sfsedssffwfdg\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def convert(self, s, numRows):\n",
    "        if numRows < 2:\n",
    "            return s\n",
    "        res = [\"\" for _ in range(numRows)]\n",
    "        i, flag = 0, -1\n",
    "        for c in s:\n",
    "            res[i] += c\n",
    "            if i == 0 or i == numRows -1:\n",
    "                flag = -flag\n",
    "            i += flag\n",
    "        return \"\".join(res)\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    Sol = Solution()\n",
    "    end = Sol.convert(\"sdfsfsdfsfgwe\",3)\n",
    "    print(end)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
